package net.zxteam.reusable.java.io;

import java.io.File;
import java.io.IOException;

import net.zxteam.reusable.java.IllegalArgumentNullException;

public final class Directory {

	public static void createDirectory(String directory) throws IOException {

		if(directory == null)
			throw new IllegalArgumentNullException("directory");

		createDirectory(new File(directory));
	}

	public static void createDirectory(String directory, boolean recursive) throws IOException {

		if(directory == null)
			throw new IllegalArgumentNullException("directory");

		createDirectory(new File(directory), recursive);
	}

	public static void createDirectory(File directory) throws IOException {

		if(directory == null)
			throw new IllegalArgumentNullException("directory");

		createDirectory(directory, false);
	}

	public static void createDirectory(File directory, boolean recursive) throws IOException {

		if(directory == null)
			throw new IllegalArgumentNullException("directory");
		
		if(!exists(directory)) {
			boolean createResult = false;
			if(recursive)
				createResult = directory.mkdirs();
			else
				createResult = directory.mkdir();
			
			if(!createResult)
				throw new IOException();
		}
	}

	public static boolean exists(String directory) {

		if(directory == null)
			throw new IllegalArgumentNullException("directory");

		return exists(new File(directory));
	}
	
	public static boolean exists(File directory) {

		if(directory == null)
			throw new IllegalArgumentNullException("directory");

		return directory.exists() && directory.isDirectory();
	}
	
	/**
	 * Create a new temporary directory. Use something like
	 * {@link #newTempDirectory()} to give java.io.File result
	 * @return the new directory
	 * @throws IOException if there is an error creating the temporary directory
	 */
	public static String newTempDirectoryStr() throws IOException { return newTempDirectory().getAbsolutePath(); }

	/**
	 * Create a new temporary directory. Use something like
	 * {@link #newTempDirectoryStr()} to give String result
	 * @return the new directory
	 * @throws IOException if there is an error creating the temporary directory
	 */
	public static java.io.File newTempDirectory() throws IOException {

		final java.io.File temp;

		temp = java.io.File.createTempFile("java.io.", Long.toString(System.nanoTime()));

		if(!(temp.delete()))
			throw new java.io.IOException("Could not delete temp file: " + temp.getAbsolutePath());

		if(!(temp.mkdir()))
			throw new java.io.IOException("Could not create temp directory: " + temp.getAbsolutePath());

		return temp;
	}

	public static void remove(String directory) throws IOException {

		if(directory == null)
			throw new IllegalArgumentNullException("directory");

		remove(new File(directory));
	}

	public static void remove(File directory) throws IOException {

		if(directory == null)
			throw new IllegalArgumentNullException("directory");

		if(!directory.exists())
			return;

		if (!directory.isDirectory())
			throw new IllegalArgumentException("Parameter 'directory' is invalid");

		String[] list = directory.list();

		// Some JVMs return null for File.list() when the directory is empty.
		if (list != null) {

			for (int i = 0; i < list.length; i++) {

				File entry = new File(directory, list[i]);

				//System.out.println("\tremoving entry " + entry);
				if (entry.isDirectory()) {
					remove(entry);
				}
				else {
					if (!entry.delete())
						return;
				}
			}
		}

		directory.delete();
	}
}